/**
  ******************************************************************************
  * @file    systick.c
  * @author  chengbb
  * @version V1.0
  * @date    2017-03-02
  * @brief   软件定时器相关操作
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2017 MindX</center></h2>
  ******************************************************************************
  */
/** @addtogroup BSP
* @{
*/
#include "stm32f10x.h"                
#include "systick.h"
//softtimer softtime;

/** 系统嘀嗒定时器 */
volatile uint32_t  g_systick_time;

/**
  ******************************************************************************
  * @brief  系统嘀嗒中断
  * @return None
  ******************************************************************************/
void SysTick_Handler(void)
{
    g_systick_time++;    
}

/**
  ******************************************************************************
  * @brief  系统嘀嗒中断初始化
  * @return None
  ******************************************************************************/
void SysTick_Init(void)
{
    SysTick_Config(SystemCoreClock / SYSTICK_PER_SECOND);
}

/**
  ******************************************************************************
  * @brief  获取当前系统时间
  * @return 系统时间
  ******************************************************************************/
uint32_t Get_Systick(void)
{
    return g_systick_time;
}

/**
  ******************************************************************************
  * @brief  获取当前系统时间
  * @param  *t 软件定时器结构指针
  * @param  interval 时间阈值
  * @return None.
  ******************************************************************************/
void Timer_Set(softtimer_t *t, uint32_t interval)
{
    t->active = 1;
    t->interval = interval;
    t->start = Get_Systick();   
}

/**
  ******************************************************************************
  * @brief  复位软件定时器
  * @param  *t 软件定时器结构指针
  * @return None.
  ******************************************************************************/
void Timer_Restart(softtimer_t *t)
{
    t->start = Get_Systick();
    t->active = 1;
}

/**
  ******************************************************************************
  * @brief  停止系统定时器
  * @param  *t 软件定时器结构指针
  * @return None.
  ******************************************************************************/
void Timer_Stop(softtimer_t *t)
{
    t->active = 0;
}

/**
  ******************************************************************************
  * @brief  判断定时器是否到时
  * @param  *t 软件定时器结构指针
  * @return 定时器结果
  ******************************************************************************/
uint32_t Timer_Expired(softtimer_t *t)
{
    if(t->active)
        return (uint32_t)(Get_Systick() - t->start) >= (uint32_t)t->interval;
    else
        return 0;
}

/**
  ******************************************************************************
  * @brief  嘀嗒定时器延时
  * @param  tick tick数
  * @return None
  ******************************************************************************/
void Timer_Delay(uint32_t tick)
{
    uint32_t t = Get_Systick();
    
    while(1)
    {
        if(Get_Systick() - t > tick)
        {
            break;
        }
    }
}

/**
  * @}
  */
/******************* (C) COPYRIGHT 2017 MINDX ******END OF FILE******************/
